<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>生命周期</title>
</head>

<body>
    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class App extends React.Component{

            state = {
                keyword: 'animal'
            }

            render(){
                return <div>
                        <button onClick={this.change('city')}>城市</button>
                        <button onClick={this.change('animal')}>动物</button>
                        <button onClick={this.change('car')}>汽车</button>
                        <hr />

                        <Image keyword={this.state.keyword} />
                    </div>
            }

            change = (type) => {
                return () => {
                    this.setState({
                        keyword: type
                    })
                }
            }
        }


        class Image extends React.Component{
            render(){
                console.log('组件正在更新中.. -- render');
                return <div>
                    <img src={"https://source.unsplash.com/500x300/?" + this.props.keyword} alt="" />
                </div>
            }

            //组件将要接收外部的属性
            componentWillReceiveProps(){
                console.log('组件将要接收属性数据 -- componentWillReceiveProps');
            }

            //组件应该要更新
            shouldComponentUpdate(){
                console.log('组件应该要更新 -- shouldComponentUpdate');
                return true;
            }

            //组件将要更新
            componentWillUpdate(){
                console.log('组件将要更新 -- componentWillUpdate');
            }

            //组件完成更新
            componentDidUpdate(){
                console.log('组件完成更新 -- componentDidUpdate');
            }

        }


        ReactDOM.render(<App />, document.querySelector("#root"));
    </script>
</body>

</html>